#pragma once

#include <string>
#include "../poco/user.hh"
#include "../repository/user-repository.hh"

namespace WordMemory
{
    class UserService
    {
    public:
        static User loginUser(const std::string& username, const std::string& password)
        {
            return UserRepository::instance().authorize(username, password);
        }

        static User registerUser(const std::string& username, const std::string& password, const std::string& real_name)
        {
            try
            {
                return UserRepository::instance().createUser(username, password, real_name);
            }
            catch (const RepositoryExcept& e)
            {
                throw ServiceExcept("Failed to register user: " + std::string(e.what()));
            }
        }

        static User getUser(int user_id)
        {
            try
            {
                return UserRepository::instance().getUser(user_id);
            }
            catch (const RepositoryExcept& e)
            {
                throw ServiceExcept("Failed to get user: " + std::string(e.what()));
            }
        }

        static User updateUser(int user_id, const std::string& real_name)
        {
            try
            {
                return UserRepository::instance().updateUser(user_id, real_name);
            }
            catch (const RepositoryExcept& e)
            {
                throw ServiceExcept("Failed to update user: " + std::string(e.what()));
            }
        }

    public:
        static UserService& instance()
        {
            static UserService service;
            return service;
        }
    };
}